package com.fun_ordering.service.impl;

import com.fun_ordering.mapper.FoPurchasingMapper;
import com.fun_ordering.mapper.MaterialStockMapper;
import com.fun_ordering.pojo.FoPurchasing;
import com.fun_ordering.pojo.FoPurchasingExample;
import com.fun_ordering.service.FoPurchasingService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fun_ordering.components.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 进货表服务实现层
 * @author XiaoPi
 *
 */
@Service
public class FoPurchasingServiceImpl implements FoPurchasingService {

	@Autowired
	private FoPurchasingMapper foPurchasingMapper;
	@Autowired
	private MaterialStockMapper materialStockMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<FoPurchasing> findAll() {
		return foPurchasingMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<FoPurchasing> page=   (Page<FoPurchasing>) foPurchasingMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(FoPurchasing foPurchasing) {
		foPurchasingMapper.insert(foPurchasing);
		materialStockMapper.updateStockNumberByStockId(foPurchasing.getMaterialId(),foPurchasing.getPurNumbe());

	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(FoPurchasing foPurchasing){
		foPurchasingMapper.updateByPrimaryKey(foPurchasing);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param purId
	 * @return
	 */
	@Override
	public FoPurchasing findOne(Integer purId){
		return foPurchasingMapper.selectByPrimaryKey(purId);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] purIds) {
		for(Integer purId:purIds){
			foPurchasingMapper.deleteByPrimaryKey(purId);
		}		
	}
	
	
		@Override
	public PageResult findPage(FoPurchasing foPurchasing, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		FoPurchasingExample example=new FoPurchasingExample();
		FoPurchasingExample.Criteria criteria = example.createCriteria();
		example.setOrderByClause("enter_time DESC");
		if(foPurchasing!=null){			
			if(foPurchasing.getPurName()!=null && foPurchasing.getPurName().length()>0){
				criteria.andPurNameLike("%"+foPurchasing.getPurName()+"%");
			};
			if(foPurchasing.getMaterialId()!=null){
				criteria.andMaterialIdEqualTo(foPurchasing.getMaterialId());
			}
		}
		
		Page<FoPurchasing> page= (Page<FoPurchasing>)foPurchasingMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
